<?php
// +-------------------------------------------------------------------------------
// | DingZone [ A Rapid Development Framework For Dingtalk ]
// +-------------------------------------------------------------------------------
// | 钉钉API请求基础类 v1.0
// |
// | Copyright (c) 2014-2018 http://www.heimalanshi.com All rights reserved.
// | Author : Jie
// | Date : 2018-08-09
// +-------------------------------------------------------------------------------

namespace ding\request;


abstract class DingApiRequest {
    /**
     * 转换为数组
     *
     * @param bool $arrayToJson
     * @return array
     * @throws \ReflectionException
     */
    public function toData($arrayToJson = false) {
        $ref = new \ReflectionClass(get_class($this));
        $properties = $ref->getProperties();

        $data = [];
        foreach ($properties as $property) {
            if ($property->isStatic()) {
                continue;
            }
            $property->setAccessible(true);

            $name = $property->getName();
            $value = $property->getValue($this);

            if (is_null($value)) {
                //null值忽略
                continue;
            }

            if (is_array($value) && $arrayToJson) {
                $data[$name] = json_encode($value);
            } else {
                $data[$name] = $value;
            }
        }
        return $data;
    }
}